package com.luck.picture.lib.compress;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Responsible for starting compress and managing active and cached resources.
 */
class Engine {

    private ExifInterface srcExif;      // 记录图片信息,判断是否旋转
    private String srcImg;              // 原图路径
    private File tagImg;                // 压缩后保存路径
    private int srcWidth;               // 原图宽
    private int srcHeight;              // 原图高

    Engine(String srcImg, File tagImg) throws IOException {
        if (Checker.isJPG(srcImg)) {
            this.srcExif = new ExifInterface(srcImg);
        }
        this.srcImg = srcImg;
        this.tagImg = tagImg;

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;// 开始读入图片设置成true
        options.inSampleSize = 1;

        BitmapFactory.decodeFile(srcImg, options);
        this.srcWidth = options.outWidth;
        this.srcHeight = options.outHeight;
    }

    /**
     * 计算压缩比例
     */
    private int computeSize() {
//        srcWidth = srcWidth % 2 == 1 ? srcWidth + 1 : srcWidth;
//        srcHeight = srcHeight % 2 == 1 ? srcHeight + 1 : srcHeight;
//
//        int longSide = Math.max(srcWidth, srcHeight);
//        int shortSide = Math.min(srcWidth, srcHeight);
//
//        float scale = ((float) shortSide / longSide);
//        if (scale <= 1 && scale > 0.5625) {
//            if (longSide < 1664) {
//                return 1;
//            } else if (longSide < 4990) {
//                return 2;
//            } else if (longSide > 4990 && longSide < 10240) {
//                return 4;
//            } else {
//                return longSide / 1280 == 0 ? 1 : longSide / 1280;
//            }
//        } else if (scale <= 0.5625 && scale > 0.5) {
//            return longSide / 1280 == 0 ? 1 : longSide / 1280;
//        } else {
//            return (int) Math.ceil(longSide / (1280.0 / scale));
//        }
        int longSide = Math.max(srcWidth, srcHeight);
        if (longSide < 1440) {// 720 * 2 = 1440
            return 1;
        } else if (longSide < 3072) {// 1024 * 3 = 3072
            return 2;
        } else {
            return 4;
        }
        /*if (longSide > 1536 && longSide <= 3072) {
            return 1 << 1;
        } else if (longSide > 3072 {
            return 1 << 2;
        } else {
            return 1;
        }*/
    }

    /**
     * 旋转图片
     */
    private Bitmap rotatingImage(Bitmap mBitmap) {
        if (null == srcExif) {
            return mBitmap;
        }
        int angle = 0;// 旋转角度
        int orientation = srcExif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
        switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                angle = 90;
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                angle = 180;
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                angle = 270;
                break;
            default:
                break;
        }
        Matrix mMatrix = new Matrix();
        mMatrix.postRotate(angle);
        return Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), mMatrix, true);
    }

    /*File compress() throws IOException {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = computeSize();

        Bitmap tagBitmap = BitmapFactory.decodeFile(srcImg, options);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();

        tagBitmap = rotatingImage(tagBitmap);
        tagBitmap.compress(Bitmap.CompressFormat.JPEG, 90, stream);
        tagBitmap.recycle();

        FileOutputStream fos = new FileOutputStream(tagImg);
        fos.write(stream.toByteArray());
        fos.flush();
        fos.close();
        stream.close();

        return tagImg;
    }*/
    File compress() throws IOException {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = computeSize();

        Bitmap tagBitmap = BitmapFactory.decodeFile(srcImg, options);
        tagBitmap = rotatingImage(tagBitmap);

        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        int quality = 100;
        tagBitmap.compress(Bitmap.CompressFormat.JPEG, quality, stream);

        int length = stream.toByteArray().length / 1024;
        if (length > 120) {
            if (length > 500) {
                quality = 70;
            } else if (length > 200) {
                quality = 80;
            } else {
                quality = 90;
            }
            stream.reset();
            tagBitmap.compress(Bitmap.CompressFormat.JPEG, quality, stream);
        }
        tagBitmap.recycle();

        FileOutputStream fos = new FileOutputStream(tagImg);
        fos.write(stream.toByteArray());
        fos.flush();
        fos.close();
        stream.flush();
        stream.close();

        return tagImg;
    }

}